<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Manage Groups
 * @description : # Place where committe members manage groups
 *                # Committe members are added in 'config/inno' as an array of usernames.
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Groups_manage extends AUTH_Controller {

  public $comitte_members = NULL;
  public $is_comitte_member = FALSE;

  /**
   * # Will set the current processing year.
   * # Checks the committe member authenticaion
   * # If current user is not comitte member, redirect back.
   * 
   */
  public function __construct() {
    parent::__construct();

    // Setting current year
    define('NOW_YEAR', date('Y'));

    // Checking permission
    $comitte_members = $this->config->item('comittee_members');
    $this->comitte_members = $comitte_members;
    if (!in_array($this->current_user['user_name'], $comitte_members)) {
      redirect('groups');
    }

    // Update group score
    $this->group_model->update_group_scores();
  }

  public function index() {
    redirect('groups_manage/group');
  }

  /**
   * Show & Add Groups
   * 
   */
  public function group() {

    // Current year groups
    $get_groups = $this->group_model->get_groups(NOW_YEAR);
    $groups = $get_groups['result'];

    // Init
    $manage_group_status = 0;

    // Checking request type
    if ($this->is_post()) {

      // Setting last updated comitte member
      $this->update_modifier();

      $manage_group_status = 2;

      // Getting & Cleaning inputs
      $group_name = $this->sanitize($this->input->post('add_group_name', TRUE));
      $get_group_by_name = $this->group_model->get_group_by_name($group_name);

      // Insert New Group
      if (!empty($group_name) && empty($get_group_by_name['result'])) {
        $set_group = $this->group_model->set_group($group_name);
        if ($set_group['status'] == 1 && !empty($set_group['result'])) {
          $manage_group_status = 1;
        }
      }

      // Refresh after procesing
      $this->session->set_flashdata('manage_group_status', $manage_group_status);
      redirect('groups_manage/group');
    }

    // Preparing & Loading view
    $view_data = array();
    $view_data['menu_active'] = 'groups';
    $view_data['groups'] = $groups;
    $this->load_view('groups_manage_group', $view_data);
  }

  /**
   * Update Group Name
   * 
   */
  public function group_update($group_id = NULL) {

    // Init status
    $manage_group_status = 2;

    // Checking request
    if ($this->is_post()) {

      // Setting last updated comitte member
      $this->update_modifier();

      // Getting inputs
      $group_name = $this->sanitize($this->input->post('update_group_name', TRUE));

      // Updating group name
      if (!empty($group_id) && is_numeric($group_id) && !empty($group_name)) {
        $update_group = $this->group_model->update_group($group_id, $group_name);
        if ($update_group['status'] == 1 && !empty($update_group['result'])) {
          $manage_group_status = 4;
        }
      }
    }

    // Refresh page
    $this->session->set_flashdata('manage_group_status', $manage_group_status);
    redirect('groups_manage/group');
  }

  /**
   * Delete Group
   * 
   */
  public function group_delete($group_id = NULL) {

    // Setting last updated comitte member
    $this->update_modifier();

    // Init status
    $manage_group_status = 2;

    // Checking group id
    if (!empty($group_id) && is_numeric($group_id)) {

      // Remove all group related stuff
      $this->group_model->remove_group_all_members($group_id);
      $this->group_model->remove_group_all_results($group_id);
      $delete_group = $this->group_model->delete_group($group_id);
      if ($delete_group['status'] == 1 && !empty($delete_group['result'])) {
        $manage_group_status = 3;
      }
    }

    // Refresh page
    $this->session->set_flashdata('manage_group_status', $manage_group_status);
    redirect('groups_manage/group');
  }

  /**
   * Manage Group Members
   * 
   */
  public function group_member() {

    // Getting current year groups
    $get_groups = $this->group_model->get_groups(NOW_YEAR);
    $groups = $get_groups['result'];

    // Injecting group members
    $count = 0;
    if (!empty($groups)) {
      foreach ($groups as $group) {
        $get_group_members = $this->group_model->get_group_members($group['group_id']);
        $groups[$count]['group_members'] = $get_group_members['result'];
        $count++;
      }
    }

    // Getting ungrouped members
    $get_ungrouped_members = $this->group_model->get_ungrouped_members(NOW_YEAR);
    $ungrouped_members = $get_ungrouped_members['result'];

    // Getting grouped members
    $get_grouped_members = $this->group_model->get_grouped_members(NOW_YEAR);
    $grouped_members = $get_grouped_members['result'];

    // Preparing view data
    $view_data = array();
    $view_data['menu_active'] = 'groups';
    $view_data['groups'] = $groups;
    $view_data['ungrouped_members'] = $ungrouped_members;
    $view_data['grouped_members'] = $grouped_members;

    // Loading view
    $this->load_view('groups_manage_group_members', $view_data);
  }

  /**
   * Add Group Member
   * 
   */
  public function group_member_add() {

    // Init status
    $manage_group_status = 2;

    // Checking request type
    if ($this->is_post()) {

      // Setting last updated comitte member
      $this->update_modifier();

      // Getting inputs
      $group_id = $this->input->post('add_members_group_id', TRUE);
      $user_ids = $this->input->post('add_members_user_id', TRUE);

      // Checking inputs
      if (!empty($group_id) && is_numeric($group_id) && !empty($user_ids) && is_array($user_ids)) {

        // Checking group exits
        $get_group_by_id = $this->group_model->get_group_by_id($group_id);
        if ($get_group_by_id['status'] == 1 && !empty($get_group_by_id)) {

          // Processing each user
          foreach ($user_ids as $user_id) {

            // Checking user exists
            $get_user_by_id = $this->user_model->get_user_by_id($user_id);
            if ($get_user_by_id['status'] == 1 && !empty($get_user_by_id['result'])) {

              // Adding user to group
              $set_group_member = $this->group_model->set_group_member($group_id, $user_id);
              if ($set_group_member['status'] == 1 && !empty($set_group_member['result'])) {
                $manage_group_status = 1;
              }
            }
          }
        }
      }
    }

    // Refresh page
    $this->session->set_flashdata('manage_group_status', $manage_group_status);
    redirect('groups_manage/group_member');
  }

  /**
   * Remove a member from a group
   * 
   */
  public function group_member_remove() {

    // Status var
    $manage_group_status = 2;

    // Checking request type
    if ($this->is_post()) {

      // Updating the modified person
      $this->update_modifier();

      // Getting user ids array
      $user_ids = $this->input->post('remove_members_user_id', TRUE);

      // Checking user ids array
      if (!empty($user_ids) && is_array($user_ids)) {

        // Removing each user from the group
        foreach ($user_ids as $user_id) {

          // Getting group of the user in current year
          $get_user_group_by_year = $this->group_model->get_user_group_by_year($user_id, NOW_YEAR);

          // Checking user is in any group
          if (!empty($get_user_group_by_year['result'])) {

            // Getting group id of user
            $group_id = (int) $get_user_group_by_year['result']['group_id'];

            // Removing user from group
            $set_group_member = $this->group_model->remove_group_member($user_id, $group_id);

            // Checking delete status
            if ($set_group_member['status'] == 1 && !empty($set_group_member['result'])) {

              // Setting status
              $manage_group_status = 3;
            }
          }
        }
      }
    }

    // Setting the status in session
    $this->session->set_flashdata('manage_group_status', $manage_group_status);

    // Redirect again to same page
    redirect('groups_manage/group_member');
  }

  /**
   * Update group member 
   * Captin, Vice captin, Comitte member
   * 
   */
  public function group_member_update() {

    // Init status var
    $manage_group_status = 2;

    // Checking request type
    if ($this->is_post()) {

      // Updating modified user
      $this->update_modifier();

      // Getting inputs
      $group_id = (int) $this->input->post('group_id', TRUE);
      $capitan_user_id = (int) $this->input->post('group_capitan_user_id', TRUE);
      $vice_capitan_user_id = (int) $this->input->post('group_vice_capitan_user_id', TRUE);
      $comitte_member_user_id = (int) $this->input->post('group_comitte_member_user_id', TRUE);

      // updating db
      $update_group_member = $this->group_model->update_group_member($group_id, $capitan_user_id, $vice_capitan_user_id, $comitte_member_user_id);
      if ($update_group_member['status'] == 1 && !empty($update_group_member['result'])) {
        $manage_group_status = 4;
      }
    }

    // Setting status session
    $this->session->set_flashdata('manage_group_status', $manage_group_status);

    // Redirect again
    redirect('groups_manage/group_member');
  }

  /**
   * List group events
   * 
   */
  public function group_event() {

    // Getting group events
    $get_group_events = $this->group_model->get_group_events(NOW_YEAR);
    $group_events = $get_group_events['result'];

    // Preparing & Loading view
    $view_data = array();
    $view_data['menu_active'] = 'groups';
    $view_data['group_events'] = $group_events;
    $this->load_view('groups_manage_group_events', $view_data);
  }

  public function group_event_add() {
    $manage_group_status = 2;
    if ($this->is_post()) {
      $this->update_modifier();
      $group_event_name = $this->sanitize($this->input->post('group_event_name', TRUE));
      $group_event_max_score = $this->sanitize($this->input->post('group_event_max_score', TRUE));
      $group_event_max_score = (is_numeric($group_event_max_score) ? $group_event_max_score : 100);
      $group_event_date = $this->sanitize($this->input->post('group_event_date', TRUE));
      if (!empty($group_event_date) && !empty($group_event_date) && strtotime($group_event_date) && !empty($group_event_max_score) && is_numeric($group_event_max_score)) {
        $set_group_event = $this->group_model->set_group_event($group_event_name, $group_event_date, $group_event_max_score);
        if ($set_group_event['status'] == 1 && !empty($set_group_event['result'])) {
          $manage_group_status = 1;
        }
      }
    }
    $this->session->set_flashdata('manage_group_status', $manage_group_status);
    redirect('groups_manage/group_event');
  }

  public function group_event_delete($group_event_id = NULL) {
    $this->update_modifier();
    $manage_group_status = 2;
    if (!empty($group_event_id) && is_numeric($group_event_id)) {
      $delete_group_event = $this->group_model->delete_group_event($group_event_id);
      if ($delete_group_event['status'] == 1 && !empty($delete_group_event['result'])) {
        $manage_group_status = 3;
      }
    }
    $this->session->set_flashdata('manage_group_status', $manage_group_status);
    redirect('groups_manage/group_event');
  }

  public function group_event_result($group_event_id = NULL) {
    $get_group_events = $this->group_model->get_group_events(NOW_YEAR);
    $group_events = $get_group_events['result'];
    $get_group_event_by_id = $this->group_model->get_group_event_by_id($group_event_id);
    $group_event_data = $get_group_event_by_id['result'];
    $get_group_event_results = $this->group_model->get_group_event_results($group_event_id);
    $group_event_results = $get_group_event_results['result'];
    $get_groups = $this->group_model->get_groups(NOW_YEAR);
    $groups = $get_groups['result'];
    $view_data = array();
    $view_data['menu_active'] = 'groups';
    $view_data['group_events'] = $group_events;
    $view_data['group_event_id'] = $group_event_id;
    $view_data['group_event_data'] = $group_event_data;
    $view_data['group_event_results'] = $group_event_results;
    $view_data['groups'] = $groups;
    $this->load_view('groups_manage_group_events_results', $view_data);
  }

  public function group_event_result_add() {
    $manage_group_status = 2;
    if ($this->is_post()) {
      $this->update_modifier();
      $group_event_id = $this->sanitize($this->input->post('group_event_id', TRUE));
      $get_group_event_by_id = $this->group_model->get_group_event_by_id($group_event_id);
      $get_group_event_results = $this->group_model->get_group_event_results($group_event_id);
      if ($get_group_event_by_id['status'] == 1 && !empty($get_group_event_by_id['result']) && empty($get_group_event_results['result'])) {
        $get_groups = $this->group_model->get_groups(NOW_YEAR);
        if ($get_groups['status'] == 1 && !empty($get_groups['result'])) {
          foreach ($get_groups['result'] as $group) {
            $group_score = $this->input->post('group_score_' . $group['group_id'], TRUE);
            $group_info = $this->input->post('group_info_' . $group['group_id'], TRUE);
            $set_group_event_result = $this->group_model->set_group_event_result($group_event_id, $group['group_id'], $group_score, $group_info);
            if ($set_group_event_result['status'] == 1 && !empty($set_group_event_result['result'])) {
              $manage_group_status = 1;
            }
          }
        }
      }
    }
    $this->session->set_flashdata('manage_group_status', $manage_group_status);
    redirect('groups_manage/group_event_result/' . $group_event_id);
  }

  public function group_event_result_update() {
    $manage_group_status = 2;
    if ($this->is_post()) {
      $this->update_modifier();
      $group_event_id = $this->sanitize($this->input->post('group_event_id', TRUE));
      $get_group_event_by_id = $this->group_model->get_group_event_by_id($group_event_id);
      $get_group_event_results = $this->group_model->get_group_event_results($group_event_id);
      if ($get_group_event_by_id['status'] == 1 && !empty($get_group_event_by_id['result']) && $get_group_event_results['status'] == 1 && !empty($get_group_event_results['result'])) {
        $get_groups = $this->group_model->get_groups(NOW_YEAR);
        if ($get_groups['status'] == 1 && !empty($get_groups['result'])) {
          foreach ($get_groups['result'] as $group) {
            $group_score = $this->input->post('group_score_' . $group['group_id'], TRUE);
            $group_info = $this->input->post('group_info_' . $group['group_id'], TRUE);
            $set_group_event_result = $this->group_model->update_group_event_result($group_event_id, $group['group_id'], $group_score, $group_info);
            if ($set_group_event_result['status'] == 1 && !empty($set_group_event_result['result'])) {
              $manage_group_status = 4;
            }
          }
        }
      }
    }
    $this->session->set_flashdata('manage_group_status', $manage_group_status);
    redirect('groups_manage/group_event_result/' . $group_event_id);
  }

  public function api_apps($limit = 99) {
    $limit = (is_numeric($limit)) ? $limit : 99;
    $get_api_apps = $this->api_model->get_api_apps($limit);
    $api_apps = $get_api_apps['result'];
    $manage_group_status = 0;
    if ($this->is_post()) {
      $this->update_modifier();
      $manage_group_status = 2;
      $app_name = $this->sanitize($this->input->post('add_app_name', TRUE));
      if (!empty($app_name)) {
        $set_group = $this->api_model->set_app($app_name, $this->current_user['user_id']);
        if ($set_group['status'] == 1 && !empty($set_group['result'])) {
          $manage_group_status = 1;
        }
      }
      $this->session->set_flashdata('manage_group_status', $manage_group_status);
      redirect('groups_manage/api_apps');
    }
    $view_data = array();
    $view_data['menu_active'] = 'groups';
    $view_data['limit'] = $limit;
    $view_data['api_apps'] = $api_apps;
    $this->load_view('groups_manage_api_apps', $view_data);
  }

  public function app_update($app_id = NULL) {
    $manage_group_status = 2;
    if ($this->is_post()) {
      $this->update_modifier();
      $app_name = $this->sanitize($this->input->post('update_app_name', TRUE));
      if (!empty($app_id) && is_numeric($app_id) && !empty($app_name)) {
        $update_app = $this->api_model->update_app($app_id, $app_name);
        if ($update_app['status'] == 1 && !empty($update_app['result'])) {
          $manage_group_status = 4;
        }
      }
    }
    $this->session->set_flashdata('manage_group_status', $manage_group_status);
    redirect('groups_manage/api_apps');
  }

  public function app_delete($app_id = NULL) {
    $this->update_modifier();
    $manage_group_status = 2;
    if (!empty($app_id) && is_numeric($app_id)) {
      $delete_app = $this->api_model->delete_app($app_id);
      if ($delete_app['status'] == 1 && !empty($delete_app['result'])) {
        $manage_group_status = 3;
      }
    }
    $this->session->set_flashdata('manage_group_status', $manage_group_status);
    redirect('groups_manage/api_apps');
  }

  /**
   * 
   * Saving last group manage accessed
   * 
   */
  private function update_modifier() {

    // Prepare settings details
    $up_details = array('user_id' => $this->current_user['user_id'], 'user_name' => $this->current_user['user_name'], 'date' => date("Y-m-d H:i:s"));

    // Add if setting not already exist
    $this->settings_model->add_settings('groups_last_update', json_encode($up_details));

    // Update the settings
    $this->settings_model->update_settings('groups_last_update', json_encode($up_details));
  }

}

?>
